package com.backend.server.service.impl;

import com.backend.server.service.ProjectInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.server.common.ErrorCode;
import com.common.server.constant.LicenseConstant;
import com.common.server.constant.ProjectConstant;
import com.common.server.entity.license.LicenseInfo;
import com.common.server.entity.license.dto.LicenseInfoQueryRequest;
import com.common.server.entity.license.vo.LicenseInfoVO;
import com.common.server.entity.project.ProjectInfo;
import com.common.server.entity.project.dto.ProjectInfoAddRequest;
import com.common.server.entity.project.dto.ProjectInfoQueryRequest;
import com.common.server.entity.project.vo.ProjectInfoVO;
import com.common.server.enums.LicenseStatusEnum;
import com.common.server.enums.ProjectStatusEnum;
import com.common.server.exception.BusinessException;
import com.common.server.exception.ThrowUtils;
import com.common.server.mapper.ProjectInfoMapper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Lenovo
 * @description 针对表【project_info(项目信息)】的数据库操作Service实现
 * @createDate 2024-12-25 11:26:25
 */
@Service
public class ProjectInfoServiceImpl extends ServiceImpl<ProjectInfoMapper, ProjectInfo>
        implements ProjectInfoService {

    /**
     * 创建项目
     *
     * @param projectInfoAddRequest
     * @return
     */
    @Override
    public Long addProject(ProjectInfoAddRequest projectInfoAddRequest) {
        // 校验
        String projectName = projectInfoAddRequest.getProjectName();
        ThrowUtils.throwIf(StringUtils.isBlank(projectName), ErrorCode.PARAMS_ERROR, "项目名不能为空");
        ThrowUtils.throwIf(projectName.length() > ProjectConstant.PROJECT_NAME, ErrorCode.PARAMS_ERROR, "项目名长度不能超过30字符");
        String projectNumber = projectInfoAddRequest.getProjectNumber();
        ThrowUtils.throwIf(StringUtils.isBlank(projectNumber), ErrorCode.PARAMS_ERROR, "项目编号不能为空");
        ThrowUtils.throwIf(projectNumber.length() > ProjectConstant.PROJECT_NUMBER, ErrorCode.PARAMS_ERROR, "项目编号长度不能超过40字符");
        String productName = projectInfoAddRequest.getProductName();
        ThrowUtils.throwIf(StringUtils.isBlank(productName), ErrorCode.PARAMS_ERROR, "所属产品不能为空");
        ThrowUtils.throwIf(productName.length() > ProjectConstant.PRODUCT_NAME, ErrorCode.PARAMS_ERROR, "所属产品长度不能超过30字符");
        String projectCode = projectInfoAddRequest.getProjectCode();
        ThrowUtils.throwIf(StringUtils.isBlank(projectCode), ErrorCode.PARAMS_ERROR, "项目代码不能为空");
        ThrowUtils.throwIf(projectCode.length() > ProjectConstant.PROJECT_CODE, ErrorCode.PARAMS_ERROR, "项目代码长度不能超过20字符");
        String manager = projectInfoAddRequest.getManager();
        ThrowUtils.throwIf(StringUtils.isBlank(manager), ErrorCode.PARAMS_ERROR, "项目经理不能为空");
        ThrowUtils.throwIf(manager.length() > ProjectConstant.MANAGER, ErrorCode.PARAMS_ERROR, "项目经理长度不能超过10字符");
        String description = projectInfoAddRequest.getDescription();
        ThrowUtils.throwIf(StringUtils.isBlank(description), ErrorCode.PARAMS_ERROR, "项目描述不能为空");
        ThrowUtils.throwIf(description.length() > ProjectConstant.DESCRIPTION, ErrorCode.PARAMS_ERROR, "项目描述长度不能超过50字符");
        Integer status = projectInfoAddRequest.getStatus();
        ThrowUtils.throwIf(ObjectUtils.isEmpty(status), ErrorCode.PARAMS_ERROR, "项目状态不能为空");
        ThrowUtils.throwIf(ObjectUtils.isEmpty(ProjectStatusEnum.getEnumByValue(status)), ErrorCode.PARAMS_ERROR, "项目状态不合法");
        // 创建项目
        ProjectInfo projectInfo = new ProjectInfo();
        BeanUtils.copyProperties(projectInfoAddRequest, projectInfo);
        boolean result = this.save(projectInfo);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "创建项目失败");
        return projectInfo.getId();
    }

    /**
     * 获取项目信息分页
     *
     * @param request
     * @return
     */
    @Override
    public Page<ProjectInfoVO> getProjectInfoPage(ProjectInfoQueryRequest request) {
        long current = request.getCurrent();
        long size = request.getSize();
        // 非空
        ThrowUtils.throwIf(current < 0 || size < 0, ErrorCode.PARAMS_ERROR, "分页条件有误");
        // 分页查询
        Page<ProjectInfo> projectInfoPage = this.page(new Page<>(current, size), getQueryWrapper(request));
        // 转换
        List<ProjectInfo> projectInfoList = projectInfoPage.getRecords();
        List<ProjectInfoVO> projectInfoVOList = this.getProjectInfoVO(projectInfoList);
        // 返回
        Page<ProjectInfoVO> projectInfoVOPage = new Page<>();
        BeanUtils.copyProperties(projectInfoPage, projectInfoVOPage);
        projectInfoVOPage.setRecords(projectInfoVOList);
        projectInfoVOPage.setTotal(projectInfoVOList.size());
        return projectInfoVOPage;
    }

    @Override
    public ProjectInfoVO getProjectInfoVO(ProjectInfo projectInfo) {
        if (projectInfo == null) {
            return null;
        }
        ProjectInfoVO projectInfoVO = new ProjectInfoVO();
        BeanUtils.copyProperties(projectInfo, projectInfoVO);
        return projectInfoVO;
    }

    @Override
    public List<ProjectInfoVO> getProjectInfoVO(List<ProjectInfo> projectInfoList) {
        if (CollectionUtils.isEmpty(projectInfoList)) {
            return new ArrayList<>();
        }
        return projectInfoList.stream().map(this::getProjectInfoVO).collect(Collectors.toList());
    }

    public QueryWrapper<ProjectInfo> getQueryWrapper(ProjectInfoQueryRequest request) {
        // 构造查询条件
        Long id = request.getId();
        String projectName = request.getProjectName();
        String projectNumber = request.getProjectNumber();
        String productName = request.getProductName();
        String projectCode = request.getProjectCode();
        String manager = request.getManager();
        String description = request.getDescription();
        Integer status = request.getStatus();
        QueryWrapper<ProjectInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(projectName), "project_name", projectName);
        // 拼接查询条件
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.like(StringUtils.isNotEmpty(projectName) && projectName.length() <= ProjectConstant.PROJECT_NAME, "project_name", projectName);
        queryWrapper.eq(StringUtils.isNotEmpty(projectNumber) && projectNumber.length() <= ProjectConstant.PROJECT_NUMBER, "project_number", projectNumber);
        queryWrapper.like(StringUtils.isNotEmpty(productName) && productName.length() <= ProjectConstant.PRODUCT_NAME, "product_name", productName);
        queryWrapper.like(StringUtils.isNotEmpty(projectCode) && projectCode.length() <= ProjectConstant.PROJECT_CODE, "project_node", projectCode);
        queryWrapper.like(StringUtils.isNotEmpty(manager) && manager.length() <= ProjectConstant.MANAGER, "manager", manager);
        queryWrapper.like(StringUtils.isNotEmpty(description) && description.length() <= ProjectConstant.DESCRIPTION, "description", description);
        queryWrapper.eq(!ObjectUtils.isEmpty(status) && !ObjectUtils.isEmpty(LicenseStatusEnum.getEnumByValue(status)), "status", status);
        // 默认排序
        String sortField = request.getSortField();
        Boolean sortOrder = request.getSortOrder();
        // 如果用户没有提供有效的排序字段，则使用默认值
        if (StringUtils.isBlank(sortField)) sortField = ProjectConstant.DEFAULT_SORT_FIELD;
        // 如果用户没有提供有效的排序方式，则使用默认值
        if (sortOrder == null) sortOrder = ProjectConstant.DEFAULT_SORT_ORDER;
        queryWrapper.orderBy(StringUtils.isNotEmpty(sortField), sortOrder, sortField);
        return queryWrapper;
    }

}




